package com.wsc.personalblog.controller;

import com.wsc.personalblog.common.Result;
import com.wsc.personalblog.entity.Category;
import com.wsc.personalblog.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api("分类相关的操作")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取到分类信息
     * @return
     */
    @GetMapping("/public")
    @ApiOperation("获取到分类信息")
    public Result<List<Category>> getCategoryInfo(){
        List<Category> categoryList = categoryService.getCategoryInfo();
        return Result.successHaveData(categoryList);
    }

    /**
     * 获取到分类信息根据分类名称
     * @return
     */
    @GetMapping("/public/{name}")
    @ApiOperation("获取到分类信息")
    public Result<List<Category>> getCategoryInfo(@PathVariable("name") String name){
        List<Category> categoryList = categoryService.getCategoryInfoByName(name);
        return Result.successHaveData(categoryList);
    }

    /**
     * 添加分类
     * 参数：名称name(必须)、描述description(非必须)
     * @param category
     * @return
     */
    @PostMapping
    @PreAuthorize("hasRole('admin')")
    public Result<Integer> addCategory(@RequestBody Category category){
        Integer integer = categoryService.addCategory(category);
        return Result.successHaveData(integer);
    }

    /**
     * 删除分类
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('admin')")
    public Result<String> deleteCategory(@PathVariable("id") Integer id){
        categoryService.deleteCategory(id);
        return Result.successNoData();
    }



    /**
     * 更新分类描述信息根据分类id
     * @param category 分类信息
     * @return
     */
    @PutMapping
    @PreAuthorize("hasRole('admin')")
    public Result<String> updateCategoryDescription(@RequestBody Category category){
        categoryService.updateCategoryDescription(category);
        return Result.successNoData();
    }
}
